package com.csxy.six.util;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {

    /**
     * 获取当前用户ID
     */
    public static Long getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() != null) {
            try {
                return Long.parseLong(authentication.getPrincipal().toString());
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    /**
     * 检查是否是管理员
     */
    public static boolean isAdmin() {
        // 实际项目中应从用户角色判断
        // 这里简化处理，实际应根据你的权限系统实现
        return true;
    }

    /**
     * 检查是否是发布者或管理员
     */
    public static boolean isOwnerOrAdmin(Long publisherId) {
        Long currentUserId = getCurrentUserId();
        if (currentUserId == null) return false;

        return currentUserId.equals(publisherId) || isAdmin();
    }
}